<?php
class Circles_Model extends CI_Model {
	
	public function getCircle($circle_id) {
		if (! is_int ( $circle_id ))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'CIRCLEID', $circle_id );
		$this->db->from ( 'CIRCLES' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		print_r ( $this->db->last_query () );
		print_r ( $query->result_array () );
		
		return $query->result_array ();
	}
	
	public function getCircles($user_id) {
		if (! is_string ( $user_id ))
			return false;
		
		$this->load->database ();
		
		$this->db->where ( 'USERID', $user_id );
		$this->db->from ( 'CIRCLES' );
		$query = $this->db->get ();
		
		$this->db->close ();
		
		print_r ( $this->db->last_query () );
		print_r ( $query->result_array () );
		
		return $query->result_array ();
	}
	
	public function addCircle( $data ) {
		$required = array ('USERID', 'NAME', 'DESCRIPTION', 'MEMBERS' );
		if (! $this->_required ( $required, $data ))
			return false;
		
		$data ['CREATETIME'] = date ( 'Y-m-d H:i:s' );
		
		$this->load->database ();
		$this->db->insert ( 'CIRCLES', $data );
		$this->db->close ();
	}
	
	public function updateCircle($circle_id, $data) {
		if (! is_int ( $circle_id ) || ! is_array ( $data ))
			return false;
		
		$this->load->database ();
		$this->db->where ( 'CIRCLEID', $circle_id );
		$this->db->update ( 'CIRCLES', $data );
		
		$this->db->close ();
		
		print_r ( $this->db->last_query () );
		
		return true;
	}
	
	public function deleteCircle($circle_id) {
		if (! is_int ( $circle_id ) )
			return false;
			
		$this->load->database();
		$this->db->delete('CIRCLES', array('CIRCLEID'=>$circle_id));
		$this->db->close();
		
		return true;
	}
}